package com.lonely.fx;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.input.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @date: 2022-07-28 17:16
 * @author: ztkj-hzb
 * @description: 自定义拖拽类型
 **/
public class CustomerDragEventSettingMain extends Application implements Serializable {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {


        AnchorPane anchorPane = new AnchorPane();

        VBox vBox1 = new VBox();
        VBox vBox2 = getVbox();
        Button button = new Button("移动");


        //TextField nameTf = new TextField();
        //TextField ageTf = new TextField();

        //ImageView imageView = new ImageView();
        //imageView.setPreserveRatio(true);

        AnchorPane.setLeftAnchor(vBox2, 300.0);
        vBox1.getChildren().addAll(button);
        //vBox2.getChildren().addAll(nameTf, ageTf, imageView);
        anchorPane.getChildren().addAll(vBox1, vBox2);
        primaryStage.setScene(new Scene(anchorPane));
        primaryStage.setWidth(700);
        primaryStage.setHeight(500);
        primaryStage.show();


        vBox2.prefWidthProperty().bind(anchorPane.widthProperty().divide(2));
        //imageView.fitWidthProperty().bind(vBox2.widthProperty());

        //TODO 自定义 DataFormat,这里要在外层定义该对象，因为如果在事件里定义执行多次会报错
        DataFormat dataFormat = new DataFormat("application/dg");
        //定义要传递的数据
        User user = new User("独孤", "25", "icons/welcome.jpg");

        button.setOnDragDetected(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                Dragboard dragboard = button.startDragAndDrop(TransferMode.COPY);
                Map<DataFormat, Object> dataFormatObjectHashMap = new HashMap<>();
                dataFormatObjectHashMap.put(dataFormat, user);
                dragboard.setContent(dataFormatObjectHashMap);

                //设置拖拽图像
                VBox vboxView = getVbox();
                ((TextField) vboxView.getChildren().get(0)).setText(user.getName());
                ((TextField) vboxView.getChildren().get(1)).setText(user.getAge());
                Image image = new Image(user.getPhoto());
                ((ImageView) vboxView.getChildren().get(2)).setImage(image);

                anchorPane.getChildren().add(vboxView);
                WritableImage snapshot = vboxView.snapshot(null, null);
                anchorPane.getChildren().remove(vboxView);

                dragboard.setDragView(snapshot);
            }
        });

        vBox2.setOnDragOver(new EventHandler<DragEvent>() {
            @Override
            public void handle(DragEvent event) {
                event.acceptTransferModes(event.getTransferMode());
            }
        });

        vBox2.setOnDragDropped(new EventHandler<DragEvent>() {
            @Override
            public void handle(DragEvent event) {

                Dragboard dragboard = event.getDragboard();

                //读取数据
                User userData = (User) dragboard.getContent(dataFormat);

                //回显
                ((TextField) vBox2.getChildren().get(0)).setText(userData.getName());
                ((TextField) vBox2.getChildren().get(1)).setText(userData.getAge());
                Image image = new Image(userData.getPhoto());
                ((ImageView) vBox2.getChildren().get(2)).setImage(image);

            }
        });

        vBox2.setOnDragEntered(new EventHandler<DragEvent>() {
            @Override
            public void handle(DragEvent event) {
                vBox2.setBorder(new Border(new BorderStroke(Color.RED, BorderStrokeStyle.DASHED, new CornerRadii(5), new BorderWidths(2))));
            }
        });

        vBox2.setOnDragExited(new EventHandler<DragEvent>() {
            @Override
            public void handle(DragEvent event) {
                vBox2.setBorder(null);
            }
        });
    }


    private VBox getVbox() {
        VBox vBox2 = new VBox();
        vBox2.setPrefWidth(200);
        vBox2.setPrefHeight(400);
        vBox2.setStyle("-fx-background-color: pink");


        TextField nameTf = new TextField();
        TextField ageTf = new TextField();

        ImageView imageView = new ImageView();
        imageView.setPreserveRatio(true);

        AnchorPane.setLeftAnchor(vBox2, 300.0);
        vBox2.getChildren().addAll(nameTf, ageTf, imageView);

        imageView.fitWidthProperty().bind(vBox2.widthProperty());

        return vBox2;
    }


    /**
     * 用户类，TODO：注意这里一定要实现 序列化
     */
    public class User implements Serializable {

        private String name;

        private String age;

        private String photo;

        public User() {
        }

        public User(String name, String age, String photo) {
            this.name = name;
            this.age = age;
            this.photo = photo;
        }

        public String getName() {
            return name;
        }

        public User setName(String name) {
            this.name = name;
            return this;
        }

        public String getAge() {
            return age;
        }

        public User setAge(String age) {
            this.age = age;
            return this;
        }

        public String getPhoto() {
            return photo;
        }

        public User setPhoto(String photo) {
            this.photo = photo;
            return this;
        }
    }
}
